package udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

public class UdpServer {
	public static void main(String[] args) throws Exception{
		DatagramSocket serverSocket = new DatagramSocket(9999);
		byte[] buffer =  new byte[256];
		
		DatagramPacket packet = new DatagramPacket(buffer,256);
		serverSocket.receive(packet);
		String ip = packet.getAddress().getHostAddress();
		int port = packet.getPort();
		System.out.println(ip+"  "+port);
		
		byte[] recBuffer = Arrays.copyOfRange(packet.getData(), 0,packet.getLength());
		String msg = new String(recBuffer,"UTF-8");
		System.out.println(msg);
		
		byte[] sendBuffer = "1 received".getBytes();
		DatagramPacket sendPacket = new DatagramPacket(sendBuffer,sendBuffer.length);
		sendPacket.setPort(port);
		sendPacket.setAddress(packet.getAddress());
		serverSocket.send(sendPacket);
		
		serverSocket.close();
	}
}
